using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
using System.Text;
using DTO;
using BUS;
using System.Text.RegularExpressions;

namespace QuanLyThuVien_Nhom23
{
    public partial class frmCapTheDocGia : Form
    {
        DocGiaBUS bus = new DocGiaBUS();
        DocGiaDTO dto;
        public frmCapTheDocGia()
        {
            InitializeComponent();            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtDiaChi.Text = "";
            txtDienThoai.Text = "";
            txtEmail.Text = "";
            txtHoTen.Text = "";
            txtMatKhau.Text = "";
            txtUserName.Text = "";
            dateTimePickerNgaySinh.Text = "";
        }

        private void frmCapTheDocGia_Load(object sender, EventArgs e)
        {

        }
        private bool KiemTraEmail(string email)
        {
            bool flag = false;
            for (int i = 0; i < email.Length; i++)
            {
                if (email[i] == '@')
                    flag = true;
            }
            return flag;
        }
        public bool KiemTraDuLieu(string username, string matkhau, string hoten, string email, string dienthoai)
        {
            bool flag = true;
            if (username == "" || matkhau == "" || hoten == "" || email == "" || dienthoai == "")
            {
                MessageBox.Show("Chua nhap du thong tin");
                return flag = false;
            }
            if (!KiemTraEmail(email))
            {
                MessageBox.Show("Email khong dung dinh dang");
                return flag = false;
            }
            Regex rx = new Regex(@"^[0-9]{9,}$");            
            if (!rx.IsMatch(dienthoai))
            {
                MessageBox.Show("Ban phai nhap 9 chu so tro len\n trong textbox dien thoai");
                return flag = false;
            }
            return flag;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            dto = new DocGiaDTO(txtUserName.Text, txtMatKhau.Text,txtHoTen.Text, txtDiaChi.Text, txtEmail.Text, dateTimePickerNgaySinh.Value, txtDienThoai.Text);
            if (!KiemTraDuLieu(dto.MaDocGia, dto.MatKhau, dto.HoTen, dto.Email, dto.DienThoai))
            {
                return;
            }
            else
            {
                bus.insert(dto);
                MessageBox.Show("Tao tai khoan thanh cong");
                this.Close();
                frmDangNhap frm = new frmDangNhap();
                frm.Show();
            }
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
